package com.kcsj.controller.basic;
//201902104050 姜瑞临
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.kcsj.domain.TaskList;
import com.kcsj.service.TaskListService;
import com.kcsj.util.JSONUtil;
import com.kcsj.util.MessageHelper;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;

@Slf4j
@WebServlet("/taskList.ctl")
public class TaskListController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取前端请求的信息
        String taskListNo = request.getParameter("taskListNo");
        Integer id= Integer.parseInt(request.getParameter("userId"));
        try {
            if(taskListNo!=null){
                this.responseTaskList(taskListNo, response);
            }else {
                this.responseTaskLists(id, response);
            }
        }catch (SQLException e){
            JSONObject message = MessageHelper.dateBaseError();
            e.printStackTrace();
            //响应message到前端
            response.getWriter().println(message);
        }catch(Exception e){
            JSONObject message = MessageHelper.netWordError();
            e.printStackTrace();
            //响应message到前端
            response.getWriter().println(message);
        }
    }
    private void responseTaskList(String taskListNo, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        //根据id查找学生
        TaskList taskList = TaskListService.getInstance().find(taskListNo);
        String order_json = JSON.toJSONString(taskList);
        JSONObject data = MessageHelper.addData(order_json);
        //响应department_json到前端
        response.getWriter().println(data);
    }
    private void responseTaskLists(Integer id,HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        //获得该ID下所有的任务单
        Collection<TaskList> taskLists = TaskListService.getInstance().findAll(id);
        String taskLists_json = JSON.toJSONString(taskLists, SerializerFeature.DisableCircularReferenceDetect);
        JSONObject data = MessageHelper.addData(taskLists_json);
        //响应departments_json到前端
        response.getWriter().println(data);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //根据request对象，获得代表参数的JSON字串
        String taskList_json = JSONUtil.getJSON(request);
        try {
            //将JSON字串解析为student对象
            TaskList taskList = JSON.parseObject(taskList_json, TaskList.class);
            TaskListService.getInstance().add(taskList);
            JSONObject message = MessageHelper.addMessage("增加成功");
            response.getWriter().println(message);
            log.info("增加成功");
        }catch (SQLException e){
            log.error(e.getMessage());
            JSONObject message = MessageHelper.dateBaseError();
            response.getWriter().println(message);
        }catch(Exception e){
            log.error(e.getMessage());
            JSONObject message = MessageHelper.netWordError();
            response.getWriter().println(message);
        }
    }

    @Override
    protected void doPut(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String taskList_json = JSONUtil.getJSON(request);
        //将JSON字串解析为TaskList对象
        TaskList taskListToAdd = JSON.parseObject(taskList_json, TaskList.class);

        //到数据库表修改TaskList对象对应的记录
        try {
            TaskListService.getInstance().update(taskListToAdd);
            JSONObject message = MessageHelper.addMessage("修改成功");
            response.getWriter().println(message);
        }catch (SQLException e){
            log.error(e.getMessage());
            JSONObject message = MessageHelper.dateBaseError();
            response.getWriter().println(message);
        }catch(Exception e){
            log.error(e.getMessage());
            JSONObject message = MessageHelper.netWordError();
            response.getWriter().println(message);
        }
    }

    @Override
    protected void doDelete(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //读取参数no
        String taskListNo = request.getParameter("taskListNo");
        //到数据库表中删除对应的任务单
        try {
            TaskListService.getInstance().delete(taskListNo);
            JSONObject message = MessageHelper.addMessage("删除成功");
            response.getWriter().println(message);
        }catch (SQLException e){
            log.error(e.getMessage());
            JSONObject message = MessageHelper.dateBaseError();
            response.getWriter().println(message);
        }
    }

}
